package br.com.infocruz.seam.crm.util;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;

public class EntityHelper {

	@SuppressWarnings("unchecked")
	public static <T> Class<T> getEntityClass(Class<T> clazz) {
		Class<T> entityClass = null;
		Type type = clazz.getGenericSuperclass();
		if (type instanceof ParameterizedType) {
			ParameterizedType paramType = (ParameterizedType) type;
			if (paramType.getActualTypeArguments().length == 2) {
				if (paramType.getActualTypeArguments()[1] instanceof TypeVariable) {
					throw new IllegalArgumentException("Could not guess entity class by reflection");
				}

				entityClass = ((Class<T>) paramType.getActualTypeArguments()[1]);
			} else {
				entityClass = ((Class<T>) paramType.getActualTypeArguments()[0]);
			}
		} else {
			throw new IllegalArgumentException("Could not guess entity class by reflection");
		}
		return entityClass;
	}
	

	
	
	
}
